home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / kocaturk / histogram.c < prev   
C/C++ Source or Header  |  1992-11-11  |  4KB  |  127 lines

  1. From ames!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!convex!seas.smu.edu!mustafa Thu Nov 12 18:40:13 PST 1992
  2. Article: 370 of comp.graphics.gnuplot
  3. Newsgroups: comp.graphics.gnuplot
  4. Path: ames!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!convex!seas.smu.edu!mustafa
  5. From: mustafa@seas.smu.edu (Mustafa Kocaturk)
  6. Subject: Histograms in Gnuplot
  7. Message-ID: <1992Nov13.014817.3123@seas.smu.edu>
  8. Summary: C filter for producing a histogram in Gnuplot
  9. Keywords: histogram insertion sort filter
  10. Sender: news@seas.smu.edu (USENET News System)
  11. Nntp-Posting-Host: turbo_f.seas.smu.edu
  12. Organization: SMU School Of Engineering and Applied Science
  13. References: <9211121849.AA13763@aulne.lifl.fr>
  14. Date: Fri, 13 Nov 1992 01:48:17 GMT
  15. Lines: 108
  16.  
  17. In article <9211121849.AA13763@aulne.lifl.fr> hemery@lifl.fr writes:
  18. >I am looking for tool like gnuplot, for drawing histogram graphics.
  19. >
  20. >Do you know where I can find this kind of tool ?
  21.  
  22. The short program below is a filter that calculates a histogram
  23. from a sequence of numbers and prints the output in such a format
  24. that it can be plotted in Gnuplot by the command sequence
  25.  
  26.     !histogram < datain > tmp
  27.     plot "tmp" with impulses
  28.  
  29. where
  30.     `histogram' is the program's name
  31.     `datain' is a file containing input data samples
  32.     `tmp' is a file to hold histogram's output
  33.  
  34. The number of points generated is 100 by default. A different
  35. number can be given as the first command line argument.
  36. Supplying a non-integer argument will cause a help message to be
  37. printed to the standard error.
  38.  
  39. Another usage example is
  40.  
  41.      !myprog | histogram 50 > tmp
  42.      plot "tmp" with impulses
  43.  
  44. The maximum number of input data points that can be handled
  45. was chosen arbitrarily to be 50000.
  46.  
  47. Yours respectfully,
  48.  
  49. Mustafa Kocaturk     mustafa@seas.smu.edu
  50.  
  51. /* ------- cut here and save the lines below as histogram.c --------- */
  52. /*
  53. This source code is placed in the public domain with no guarantee
  54. of fitness for a particular purpose or of portability, but
  55. only with the hope that it may be useful.
  56.  
  57. I wrote `histogram.c' as a part of my effort to contribute
  58. to a paper by two of my colleagues at SMU. The paper
  59. discussed various programming environments such as Gnuplot,
  60. awk, spice, and xfig.
  61.  
  62. Mustafa Kocaturk    mustafa@seas.smu.edu  EE Dept., SEAS, SMU
  63. */
  64. #include <stdio.h>
  65. #include <math.h>
  66. #define HELP \
  67. "This program calculates the histogram of a sequence of numbers\n\
  68. read from the standard input until an end of file is encountered.\n\
  69. The results are printed on the standard output as lines of the format\n\
  70. <subinterval midpoint> <number of sample points in subinterval>\n\
  71. \n\
  72. Usage: %s [number of subintervals (default==100)]\n" /* Message string */
  73.  
  74. #define CALSIZ    50000 /* Number of doubles to be allocated for sort */
  75.  
  76. main(argc,argv)
  77.  int argc;
  78.  char **argv;
  79.  {
  80.  double x,t,z,*a,*b,*f,*fe,*fl;
  81.  unsigned i=0,k=100;
  82.  int j=1;
  83.  char c;
  84.  if(argc>1)j=sscanf(argv[1],"%u",&k);
  85.  if(j!=1){fprintf(stderr,HELP,argv[0]);return 0;}
  86.  if(NULL==(a=(double *)calloc(CALSIZ,sizeof x)))
  87.   {
  88.   fprintf(stderr,"%s: Unable to allocate memory\n",(argv[0]));
  89.   return 0; /* let other work continue */
  90.   }
  91.  fl=a+CALSIZ-1;
  92.  fe=a;
  93.  b=a+1;
  94.  for(i=0;j>0&&fe<fl;)
  95.   {
  96.   if((j=scanf("%lf%*[^\n]%*c",&x))>0) /* If number was recognized */
  97.    {
  98.    f=fe++;
  99.    while(x<*f&&f>a) { f[1]=*f; f--; }
  100.    f[1]=x;
  101.    i++;
  102.    }
  103.   else
  104.    j=scanf("%*[^\n]%*c");
  105.    /* Read data until end-of-line,
  106.    if end-of-stdin (i. e. ctrl-D) then j<0 (i. e. break the for loop) */
  107.   }
  108.  x=*b; /* This was Insertion sort (increasing) */
  109.  t=*fe;
  110.  printf("# %d points in [%lg, %lg]; Frequencies in %d subintervals:\n",
  111.   i,x,t,k);
  112.  t=(t-x)/k;
  113.  x+=t;
  114.  for(f=b,i=0;i<k;i++,x+=t)
  115.   {
  116.   for(j=0;x>*f&&f<=fe;j++,f++);/* Count points in the subinterval */
  117.   printf(" %l g %d\n",x-t*.5,j); /* Print subinterval center and
  118.             number of points in the subinterval to stdout */
  119.   }
  120.  }
  121. /* ------- cut here and save the lines above as histogram.c --------- */
  122. -- 
  123. Mustafa KOCATURK    mustafa@seas.smu.edu  EE Dept., Room 305A, Caruth Bldg.
  124. Home: 214-706-5954  Office: 214-768-1475  SMU Box 753190, Dallas, TX 75275
  125.  
  126.  
  127.